package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JiCaiQuotedPriceCompanyEntity;
import com.sgb.jicai.view.fragment.JiCaiQuotedPriceFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class JiCaiQuotedPriceFraModel extends ViewModel {

    private JiCaiQuotedPriceFragment fragment;

    public void getQuotedCompanyList(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getQuotedPriceCompanyList(id).enqueue(new Callback<BaseEntity<List<JiCaiQuotedPriceCompanyEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<JiCaiQuotedPriceCompanyEntity>>> call, Response<BaseEntity<List<JiCaiQuotedPriceCompanyEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(fragment.getContext(), "获取报价公司失败");
                    return;
                }
                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    fragment.updateListView(response.body().getData());
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<JiCaiQuotedPriceCompanyEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    public void setActivity(JiCaiQuotedPriceFragment jiCaiQuotedPriceFragment) {
        this.fragment = jiCaiQuotedPriceFragment;
    }
}
